package com.yiwei.zhh.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yiwei.zhh.R;
import com.yiwei.zhh.api.callback.NickNameContract;
import com.yiwei.zhh.api.requestInfo.NickNameRequest;
import com.yiwei.zhh.constant.UserGlobal;
import com.yiwei.zhh.util.PreferenceUtils;
import com.yiwei.zhh.constant.Const;
import com.yiwei.zhh.event.OnHomeEventMsg;
import com.yiwei.zhh.model.ResponseModel;
import com.yiwei.zhh.util.ToastUtil;
import com.yiwei.zhh.view.CircularProgress;

import org.greenrobot.eventbus.EventBus;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 修改昵称界面
 * Created by Master.Dong on 16/4/10.
 */
public class NicknameActivity extends BaseActivity implements View.OnClickListener, NickNameContract.View {

    private TextView tishi, btn;
    private EditText nickname;
    private CircularProgress loading;
    private NickNameRequest api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.nickname_activity);
        baseInitView(this, "NickameActivity", Const.ViewType.ACTIVITY);
        initView();

    }

    private void initView() {
        api = NickNameRequest.create();
        api.attachView(this);
        setBackIntent();
        setTitleText("修改昵称");

        loading = (CircularProgress) findViewById(R.id.progressBar);
//        nicknameTv = (TextView) findViewById(R.id.nickname_tv);
        nickname = (EditText) findViewById(R.id.nickname);
        tishi = (TextView) findViewById(R.id.tishi);
        btn = (TextView) findViewById(R.id.btn);

        btn.setOnClickListener(this);

        setNickname();
    }

    /**
     * 设置昵称
     */
    private void setNickname() {
        if (UserGlobal.hasUsername()) {
            btn.setVisibility(View.GONE);
            btn.setEnabled(false);
            nickname.setText(PreferenceUtils.getString(this, "username", ""));
            nickname.setEnabled(false);
            nickname.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray));
            tishi.setText(getResources().getString(R.string.update_nickname_text2));
        } else {
            btn.setVisibility(View.VISIBLE);
            btn.setEnabled(true);
            nickname.setEnabled(true);
            nickname.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_dark));
            tishi.setText(getResources().getString(R.string.update_nickname_text1));
        }
    }

    /**
     * 更新广播
     */
    private void sendBroadcast() {
//        Intent mIntent = new Intent(Const.HOME_VIEW_ACTION);
//        mIntent.putExtra("type", Const.SendType.UPDATE);
//        sendBroadcast(mIntent);

        Map<String, String> map = new HashMap<>();
        map.put("type", Const.SendType.UPDATE);
        EventBus.getDefault().post(new OnHomeEventMsg(map));
    }


    /**
     * 获取修改昵称内容
     */
    private void getUpdateNickname() {
        String name = "";
        if (!TextUtils.isEmpty(nickname.getText().toString())) {
            try {
                name = URLEncoder.encode(nickname.getText().toString(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        api.getNickname(name);
    }

    /**
     * 是否显示加载框
     *
     * @param b
     */
    private void showLoading(boolean b) {
        if (loading != null) {
            if (b)
                loading.setVisibility(View.VISIBLE);
            else
                loading.setVisibility(View.GONE);
        }
    }

    private boolean checkNickname() {
        if (TextUtils.isEmpty(nickname.getText().toString().trim())) {
            ToastUtil.makeText(this, getResources().getString(R.string.please_input_nickname), Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                if (checkNickname())
                    getUpdateNickname();
                break;

        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getNicknameSuccess(ResponseModel model) {
        PreferenceUtils.putBoolean(getApplicationContext(), "has_user_name", true);
        PreferenceUtils.putString(getApplicationContext(), "username", nickname.getText().toString().trim());
        ToastUtil.makeText(getApplicationContext(), model.getMsg(), Toast.LENGTH_SHORT).show();
        sendBroadcast();
        NicknameActivity.this.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
    }
}
